package com.quality.web.controller.monitor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.quality.common.annotation.Log;
import com.quality.common.constant.Constants;
import com.quality.common.core.controller.BaseController;
import com.quality.common.core.domain.AjaxResult;
import com.quality.common.core.domain.model.LoginQualitygGroup;
import com.quality.common.core.page.TableDataInfo;
import com.quality.common.core.redis.RedisCache;
import com.quality.common.enums.BusinessType;
import com.quality.common.utils.StringUtils;
import com.quality.system.domain.SysQualitygGroupOnline;
import com.quality.system.service.ISysQualitygGroupOnlineService;

/**
 * 在线用户监控
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/online")
public class SysQualitygGroupOnlineController extends BaseController
{
    @Autowired
    private ISysQualitygGroupOnlineService QualitygGroupOnlineService;

    @Autowired
    private RedisCache redisCache;

    @PreAuthorize("@ss.hasPermi('monitor:online:list')")
    @GetMapping("/list")
    public TableDataInfo list(String ipaddr, String QualitygGroupName)
    {
        Collection<String> keys = redisCache.keys(Constants.LOGIN_TOKEN_KEY + "*");
        List<SysQualitygGroupOnline> QualitygGroupOnlineList = new ArrayList<SysQualitygGroupOnline>();
        for (String key : keys)
        {
            LoginQualitygGroup QualitygGroup = redisCache.getCacheObject(key);
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(QualitygGroupName))
            {
                if (StringUtils.equals(ipaddr, QualitygGroup.getIpaddr()) && StringUtils.equals(QualitygGroupName, QualitygGroup.getQualitygGroupname()))
                {
                    QualitygGroupOnlineList.add(QualitygGroupOnlineService.selectOnlineByInfo(ipaddr, QualitygGroupName, QualitygGroup));
                }
            }
            else if (StringUtils.isNotEmpty(ipaddr))
            {
                if (StringUtils.equals(ipaddr, QualitygGroup.getIpaddr()))
                {
                    QualitygGroupOnlineList.add(QualitygGroupOnlineService.selectOnlineByIpaddr(ipaddr, QualitygGroup));
                }
            }
            else if (StringUtils.isNotEmpty(QualitygGroupName) && StringUtils.isNotNull(QualitygGroup.getQualitygGroup()))
            {
                if (StringUtils.equals(QualitygGroupName, QualitygGroup.getQualitygGroupname()))
                {
                    QualitygGroupOnlineList.add(QualitygGroupOnlineService.selectOnlineByQualitygGroupName(QualitygGroupName, QualitygGroup));
                }
            }
            else
            {
                QualitygGroupOnlineList.add(QualitygGroupOnlineService.loginQualitygGroupToQualitygGroupOnline(QualitygGroup));
            }
        }
        Collections.reverse(QualitygGroupOnlineList);
        QualitygGroupOnlineList.removeAll(Collections.singleton(null));
        return getDataTable(QualitygGroupOnlineList);
    }

    /**
     * 强退用户
     */
    @PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public AjaxResult forceLogout(@PathVariable String tokenId)
    {
        redisCache.deleteObject(Constants.LOGIN_TOKEN_KEY + tokenId);
        return AjaxResult.success();
    }
}
